package employee.web.beans;

import java.util.LinkedList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;

import customer.ejb.eaos.PersonalizedTPInterface;
import customer.ejb.eaos.PredefinedTPOffer;
import dtos.ExcursionDTO;
import dtos.HotelDTO;
import dtos.MeanOfTransportationDTO;
import dtos.PersonalizedTPDTO;
import dtos.PredefinedTPDTO;
import dtos.TransportationTypeDTO;
import dtos.TripDTO;
import employee.ejb.management.PackageMgr;
import employee.ejb.management.ProductMgr;



@ManagedBean(name="productBean")
@ViewScoped
public class ProductBean {
	
	@EJB
	private ProductMgr prodMgr;
	@EJB
	private PredefinedTPOffer predTPMgr;
	@EJB
	private PackageMgr packageMgr;
	@EJB
	private PersonalizedTPInterface persTPMgr;

	private HotelDTO hotel;
	private ExcursionDTO excursion;
	private TripDTO trip;
	private TransportationTypeDTO type;
	private MeanOfTransportationDTO mean;

	
	@PostConstruct
	public void init(){
		hotel = new HotelDTO();
		excursion = new ExcursionDTO();
		trip = new TripDTO();
		type = new TransportationTypeDTO();
		mean = new MeanOfTransportationDTO();
	}
	

	public HotelDTO getHotel() {
		return hotel;
	}
	
	public void setHotel(HotelDTO hotel) {
		this.hotel = hotel;
	}
	
	public ExcursionDTO getExcursion(){
		return excursion;
	}
	
	public void setExcursion(ExcursionDTO excursion){
		this.excursion = excursion;
	}
	
	public TripDTO getTrip(){
		return trip;
	}
	
	public void setTrip(TripDTO trip){
		this.trip = trip;
	}
	
	public TransportationTypeDTO getType(){
		return type;
	}
	
	public void setType(TransportationTypeDTO type){
		this.type = type;
	}

	public MeanOfTransportationDTO getMean(){
		return mean;
	}
	
	public void setMean(MeanOfTransportationDTO mean){
		this.mean = mean;
	}
	
	public String createHotel() {
		prodMgr.saveHotel(hotel);
		return "manageHotel?faces-redirect=true";
	}
	
	public String createExcursion(){
		prodMgr.saveExcursion(excursion);
		return "manageExcursion?faces-redirect=true";
	}
	
	public String createTrip(){
		prodMgr.saveTrip(trip);
		
		return "manageTransportation?faces-redirect=true";
	}
	
	public String createType(){
		prodMgr.saveType(type);
		
		return "manageTransportation?faces-redirect=true";
	}
	
	public String createMean(){
		prodMgr.saveMean(mean);
		
	return "manageTransportation?faces-redirect=true";
	}
	
	public void setMeanTrip(TripDTO trip){
		mean.setTrip(trip);
		
		
	}
	
	public void setMeanType(TransportationTypeDTO type){
		mean.setTransportationType(type);
		
	}
	
	public void deleteHotel(HotelDTO hotel){
		for (PredefinedTPDTO tp : predTPMgr.getPredefinedTPOffer()){
			if (tp.getHotelBean().getHotelID() == hotel.getHotelID()){				
				packageMgr.removePredTP(tp.getID());
			}
		}
		for (PersonalizedTPDTO tp : persTPMgr.getAllPersonalizedTP()){
			if (tp.getHotelBean().getHotelID() == hotel.getHotelID()){				
					persTPMgr.removePersTP(tp.getTravelID());
			}
		}
		prodMgr.removeHotel(hotel.getHotelID());
		
		FacesMessage msg = new FacesMessage("Hotel deleted");  
		  
        FacesContext.getCurrentInstance().addMessage(null, msg);
		
	}
	
	public void deleteExcursion(ExcursionDTO exc){
		for (PredefinedTPDTO tp : predTPMgr.getPredefinedTPOffer()){
			boolean delete = false;
			for (ExcursionDTO e : tp.getExcursions()){			
				if ((exc.getExcursionID() == e.getExcursionID())){
					delete = true;
				}
			}
			if (delete){
				packageMgr.removePredTP(tp.getID());
			}
			
		}
	
		for (PersonalizedTPDTO tp : persTPMgr.getAllPersonalizedTP()){
			boolean delete = false;
			for (ExcursionDTO e : tp.getExcursions()){			
				if ((exc.getExcursionID() == e.getExcursionID())){
					delete = true;
				}
			}
			if (delete){
				persTPMgr.removePersTP(tp.getTravelID());
			}
			
		}

		
		prodMgr.removeExcursion(exc.getExcursionID());
		
		FacesMessage msg = new FacesMessage("Excursion deleted");  
		  
        FacesContext.getCurrentInstance().addMessage(null, msg);
        
		
	}
	
	public void deleteTrip(TripDTO trip, DataModel<MeanOfTransportationDTO> means){
		for(MeanOfTransportationDTO mean : means){
			if(mean.getTrip().getTripID() == trip.getTripID()){
				deleteMean(mean);
			}
		}
		
		prodMgr.removeTrip(trip.getTripID());

		
		FacesMessage msg = new FacesMessage("Trip deleted");  
		  
        FacesContext.getCurrentInstance().addMessage(null, msg);
	}
	
	public void deleteType(TransportationTypeDTO type, DataModel<MeanOfTransportationDTO> means){
		for(MeanOfTransportationDTO mean : means){
			if(mean.getTransportationType().getTransportationTypeID() == type.getTransportationTypeID()){
				deleteMean(mean);
			}
		}
		prodMgr.removeType(type.getTransportationTypeID());
		FacesMessage msg = new FacesMessage("transportation type deleted");  
		  
        FacesContext.getCurrentInstance().addMessage(null, msg);
	}
	
	public void deleteMean(MeanOfTransportationDTO mean){
		for (PredefinedTPDTO tp : predTPMgr.getPredefinedTPOffer()){
			if ((tp.getMeanOfTransportation1().getTransportationID() == mean.getTransportationID())||(tp.getMeanOfTransportation2().getTransportationID() == mean.getTransportationID())){
				packageMgr.removePredTP(tp.getID());
			}
		}
		for (PersonalizedTPDTO tp : persTPMgr.getAllPersonalizedTP()){
			if ((tp.getMeanOfTransportation1().getTransportationID() == mean.getTransportationID())||(tp.getMeanOfTransportation2().getTransportationID() == mean.getTransportationID())){
				persTPMgr.removePersTP(tp.getTravelID());
			}
		}
		prodMgr.removeMean(mean.getTransportationID());
		
		FacesMessage msg = new FacesMessage("mean of transportation deleted");  
		  
        FacesContext.getCurrentInstance().addMessage(null, msg);
	}
	
}


